package com.chazz.dao.impl;

import com.chazz.dao.OrderDao;
import com.chazz.entity.Orderdetails;
import com.chazz.entity.Orders;
import com.chazz.utils.DBUtils;
import com.mysql.cj.protocol.Resultset;

import java.sql.ResultSet;
import java.sql.SQLException;

public class OrderDaoImpl extends GenericDaoImpl<Orders,Integer> implements OrderDao {


    @Override
    public void orderAdd(Orders order) {
        String sql = "INSERT INTO orders(id,uid,tatolprice,state,isdelete,createtime,orderadress)VALUES(?,?,?,?,?,?,?)";
        Object[] param = new Object[]{
                order.getId(),
                order.getUid(),
                order.getTatolprice(),
                order.getState(),
                order.getIsdelete(),
                order.getCreatetime(),
                order.getOrderadress()
        };
        DBUtils.executeUpdate(sql,param);
    }

    @Override
    public Orders findByUid(Integer userid) {
        Orders order = null;
        String sql = "SELECT * FROM Orders WHERE id=?";
        Object[] param = new Object[]{
                userid
        };
        ResultSet rs = DBUtils.executeQuery(sql,param);
        try{
            while(rs.next()){
                order = reflectUtils.newInstance(Orders.class,rs);
            }
        }catch(SQLException ex){
            ex.printStackTrace();
        }
        return order;
    }

    @Override
    public void orderdetailsAdd(Orderdetails orderdetail) {
        String sql = "INSERT INTO orderdetails(oid,gid,count,tatolprice)VALUES(?,?,?,?)";
        Object[] param = new Object[]{
                orderdetail.getOid(),
                orderdetail.getGid(),
                orderdetail.getCount(),
                orderdetail.getTatolprice()
        };
        DBUtils.executeUpdate(sql,param);
    }
}
